package org.jb.coinche.client.gui.user;


import org.jb.coinche.client.user.LoginPanel;
import org.jb.coinche.client.user.LoginPanel.LoginPanelListener;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.Layer.ShadowPosition;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;

/**
 * 
 * @author jeremie
 *
 */
public class LoginDialog extends Dialog {

	public LoginDialog( LoginPanelListener listener ) {
		this.listener = listener;
		
	    FormLayout layout = new FormLayout();
	    layout.setLabelWidth(90);
	    layout.setDefaultWidth(155);
	    setLayout(layout);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    setModal(true);
	    setBodyBorder(true);
	    setBodyStyle("padding: 8px;background: none");
	    setWidth(300);
	    setResizable(false);
	    setClosable(false);
	    setShadowOffset(10);
	    setShadowPosition(ShadowPosition.DROP);

	    KeyListener keyListener = new KeyListener() {
	      public void componentKeyUp(ComponentEvent event) {
	        validate();
	        
	        if (event.getKeyCode() == 13 && login.isEnabled())
	        	onSubmit();
	      }

	    };

	    userName = new TextField<String>();
	    userName.setFieldLabel("Username");
	    userName.addKeyListener(keyListener);
	    add(userName);

	    password = new TextField<String>();
	    password.setPassword(true);
	    password.setFieldLabel("Password");
	    password.addKeyListener(keyListener);
	    add(password);

	    setFocusWidget(userName);
	}
	
	/**
	 * 
	 */
	@Override
	protected void createButtons() {
		super.createButtons();
		status = new Status();
		status.setBusy("Connection...");
		status.hide();
		status.setAutoWidth(true);
		getButtonBar().add(status);

		getButtonBar().add(new FillToolItem());

		reset = new Button("Reset");
		reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				userName.reset();
				password.reset();
				validate();
				userName.focus();
			}

		});

		login = new Button("Login");
		login.disable();
		login.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				onSubmit();
			}
		});

		addButton(reset);
		addButton(login); 
	}
	
	/**
	 * 
	 */
	protected void onSubmit() {
		status.show();
		getButtonBar().disable();
		
		listener.onLogin( userName.getValue(), password.getValue() );
	}
	
	public void failed() {
		status.hide();
		getButtonBar().enable();
		
		MessageBox.alert("Erreur", "Mot de passe invalide", null );
	}
	
	/**
	 * 
	 * @param field
	 * @return
	 */
	protected boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}

	/**
	 * 
	 */
	protected void validate() {
		login.setEnabled( hasValue(userName) ); 
	}
	
	private TextField<String> userName;
	private TextField<String> password;
	private Button reset;
	private Button login;
	private Status status;

	private LoginPanel.LoginPanelListener listener;
}